using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class Dialog1Controller :UI.BaseController
    {

        public ActionResult Index()
        {
            return View();
        }

        public string List(int? p)
        {
            int page = 0; if (p != null) page = p.Value;
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.Dialog), "DialogCode,Title");
            var bll = new BLL.Dialog();
            var count=bll.GetCount(where);
            if(page>=count) page=count-1;
            var list = bll.Get(where, null, page,1);
            Common.Result ret = new Common.Result(false, 0, "未查询到任何数据");
            if (list.Count > 0)
            {
                ret.success = true;
                ret.count = count;
                ret.data = new
                {
                    DialogCode = list[0].DialogCode,
                    Title = list[0].Title,
                    HQL = list[0].HQL
                };
                ret.message = "查询成功";
            }
            return UI.Utils.ToJsonString(ret);
        }

        public string Delete(string id)
        {
            var ret = new BLL.Dialog().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        public string Add(Model.Dialog model)
        {
            var ret=new BLL.Dialog().Add(model);
            return UI.Utils.ToJsonString(ret);
            
        }

        public ActionResult Edit(string  DialogCode)
        {
            var model = new BLL.Dialog().Get(DialogCode);
            return View(model);
        }

        [HttpPost]
        public string Edit(Model.Dialog model)
        {
            var ret = new BLL.Dialog().Update(model);
            return UI.Utils.ToJsonString(ret);
        }


        public string DetailsList(string DialogCode)
        {
            var list = new BLL.DialogField().Get("DialogCode=?", null, 0, 0, DialogCode);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    id = i.ID,
                    FieldKey = i.FieldKey,
                    Title = i.Title,
                    Width = i.Width,
                    Sort = i.Sort,
                    IsShow = i.IsShow
                });
            }
            var ret = new
            {
                total = list.Count,
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        public string DetailsAdd(Model.DialogField model)
        {
            var ret = new BLL.DialogField().Add(model,false);
            return UI.Utils.ToJsonString(ret);
        }
    }
}
